import time
import execjs
import requests
import json
import pymysql
from tqdm import tqdm
from collections import defaultdict


def get_response():
    token = '2f8991aa1f21249783ef0bbc833cb498'
    i = int(time.time() * 1000)
    g = '12574478'
    data = '{"cid":"TpFacRecommendService:TpFacRecommendService","methodName":"execute","params":"{\\"query\\":\\"mainCate=10166&leafCate=\\",\\"sort\\":\\"mix\\",\\"pageNo\\":\\"1\\",\\"pageSize\\":\\"20\\",\\"from\\":\\"PC\\",\\"trafficSource\\":\\"pc_index_recommend\\"}"}'
    sign_key = token + '&' + str(i) + '&' + g + '&' + data

    with open('1688.js', 'r', encoding='UTF-8') as f:
        jscode = f.read()
    sign = execjs.compile(jscode).call('h', sign_key)
    # 请求API
    url = 'https://h5api.m.1688.com/h5/mtop.taobao.widgetservice.getjsoncomponent/1.0/'
    params = {
            'jsv': '2.6.1',
            'appKey': '12574478',
            't': i,
            'sign': sign,
            'v': '1.0',
            'type': 'jsonp',
            'isSec': 0,
            'timeout': 20000,
            'api': 'mtop.taobao.widgetService.getJsonComponent',
            'dataType': 'jsonp',
            'jsonpIncPrefix': 'mboxfc',
            'callback': 'mtopjsonpmboxfc3',
            'data': data
    }
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0',
        'cookie': 'cna=fAVTH42K5C8CAdIqlw27WN91; __cn_logon__=false; mtop_partitioned_detect=1; _m_h5_tk=2f8991aa1f21249783ef0bbc833cb498_1724946160970; _m_h5_tk_enc=1e2b81c5e907b3187ae4ecd3780ca424; xlly_s=1; cookie2=15e41dce33aa7c2b052b3c414d17bf31; t=f10c3f9baca06265035cb6b5cfae2f12; _tb_token_=534737ebbe383; isg=BOnpxoqJq8jNhJdg7POM6CO2-JVDtt3owtxvXoveZVAPUglk0wbtuNdEFfbkeHUg; tfstk=fgcSpAAdJgj58XLvRML4hNiVw_VB7DOZ2waKS2CPJ7F8vWimfJU8J6QxhDn0UuLHEkaKD27z23vuJ6itmy7rJXzKOmV8KYrU4kNI-cT2bCRZr4V3ph-w_ApbE6P3vBPLjveu1WKwbIWVk8xz9vrGLNjjko4QJyFKvsBYRoCdpDE8HiU_SWFL9DExkPa8JMB8ei3YmyFLvDFKwmdbuUZmPEH4OU_C3lg8c63rGze5E4Ef96hjm8ZObo1d9jaSognmp19UXAqzQlmvg_NIkyibQYt1NDgKSvejOgLrX2noG7V6LU2sHxkaFxtROolzRRFLhwCKlSZmfjP96iamH4kKZ0_C9zcr_JZgheC3LShaBAn5-F0Ywyn3IX-GZu3KSfDa1QsgPx3_Ggo1_l9Re9_bI6abbETfK9vjNvVOXD1w28U0uG8XlOu3er4bbETfK928or8wlE6Z-',
        'referer': 'https://sale.1688.com/'
    }
    response = requests.get(url, params=params, headers=headers)
    parse_and_getdata(response)


def parse_and_getdata(response):
    json_str = response.text[len(' mtopjsonpmboxfc3('):-1]
    data = json.loads(json_str)
    fac_list = data['data']['result']['facList']
    result = []
    for i in tqdm(range(len(fac_list))):
        fac = fac_list[i]
        detail = defaultdict(str)
        fac_name = fac['facName']
        medal = fac['medal']
        productionService = fac['productionService']
        location = fac['location']
        try:
            factoryControlAcreage = fac['factoryControlAcreage']
        except:
            factoryControlAcreage = 0
        try:
            productorNum = fac['productorNum']
        except:
            productorNum = 0
        detail['fac_name'] = fac_name
        detail['medal'] = medal
        detail['productionService'] = productionService
        detail['location'] = location
        detail['factoryControlAcreage'] = factoryControlAcreage
        detail['productorNum'] = productorNum
        result.append(detail)
    save(result)


def save(result):
    for data in result:
        insert(data['fac_name'], data['medal'], data['productionService'], data['location'], data['factoryControlAcreage'], data['productorNum'])


def init_db():
    try:
        conn = pymysql.connect(
            host='localhost',
            port=3306,
            user='root',
            password='root',
            database='1688fac',
        )
        c = conn.cursor()
        sql = '''
                    CREATE TABLE 1688fac (
                    id INT PRIMARY KEY AUTO_INCREMENT,
                    facName TEXT NOT NULL,
                    medal TEXT NOT NULL,
                    productionService TEXT NOT NULL,
                    location TEXT NOT NULL,
                    factoryControlAcreage INT NOT NULL ,
                    productorNum INT NOT NULL
                    );
                  '''
        c.execute(sql)
        conn.commit()
        conn.close()
    except:
        pass


def insert(fac_name, medal, productionService, location, factoryControlAcreage, productorNum):
    conn = pymysql.connect(
        host='localhost',
        port=3306,
        user='root',
        password='root',
        database='1688fac',
    )
    cur = conn.cursor()
    sql = '''insert into 1688fac(facname, medal, productionService, location, factoryControlAcreage, productorNum)
             values ('%s', '%s', '%s', '%s', '%s', '%s') ''' % (
             fac_name, medal, productionService, location, factoryControlAcreage, productorNum)
    cur.execute(sql)
    conn.commit()
    conn.close()


if __name__ == '__main__':
    init_db()
    get_response()
